package com.yunshouhu.pkid;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;


/**
 * 工具类
 * 
 * @author lw
 * 
 */
public class Tool {
	/**
	 * 分隔特殊字符串
	 */
	// public static String separator
	// =System.getProperty("line.separator")+"*#@";
	public static String separator = "\n*#@";

	private static String projectPath;

	private static HashMap<String, String> configureByTag = new HashMap<String, String>();

	// APK使用了其他家加密是否影响检测结果
	public static final boolean ISDISTINENCRYPTION = false;


	/**
	 * 按照百分比读取文件指定信息
	 * 
	 * @param 文件地址
	 * @param 读取百分比
	 *            (从头开始算)
	 * @return
	 * @throws Exception
	 */

	public static String reader(String filePath, float readerPercentage)
			throws Exception {
		return reader(filePath, "UTF-8", readerPercentage);
	}

	/**
	 * 读取文件指定文件的信息
	 * 
	 * @param 文件地址
	 * @return
	 * @throws Exception
	 */
	public static String reader(String filePath) throws Exception {
		return reader(filePath, "UTF-8");
	}

	/**
	 * 读取文件指定文件指定字符集编码的信息
	 * 
	 * @param 文件地址
	 * @param 字符串编码
	 * @return
	 * @throws Exception
	 */
	public static String reader(String filePath, String charset,
			float readerPercentage) throws Exception {
		BufferedInputStream reader = new BufferedInputStream(
				new FileInputStream(filePath));
		long fileSize = new File(filePath).length();
		int readerSize = (int) (fileSize * readerPercentage);
		byte[] buffer = new byte[readerSize];
		reader.read(buffer);
		reader.close();
		return new String(buffer, charset);
	}

	/**
	 * 读取文件指定文件指定字符集编码的信息
	 * 
	 * @param 文件地址
	 * @param 字符串编码
	 * @return
	 * @throws Exception
	 */
	public static String reader(String filePath, String charset)
			throws Exception {
		StringBuffer sb = new StringBuffer();
		BufferedReader reader = new BufferedReader(new InputStreamReader(
				new FileInputStream(new File(filePath)), charset));
		String readerLine = null;
		while ((readerLine = reader.readLine()) != null) {
			sb.append(readerLine + "\n");
		}
		reader.close();
		return sb.toString();
	}

	/**
	 * 对指定文件写入指定信息
	 * 
	 * @param 写入的文件地址
	 * @throws Exce
	 */
	public static void writer(String filePath, String content) throws Exception {
		writer(filePath, content, "UTF-8", false);
	}

	/**
	 * 对指定文件按照指定字符集编码与是否追加方式进行写入指定信息
	 * 
	 * @param 写入的文件地址
	 * @param 字符集编码
	 * @param 是否追加
	 * @throws Exce
	 */
	public static void writer(String filePath, String Content, String charset,
			boolean isAppend) throws Exception {
		BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
				new FileOutputStream(new File(filePath), isAppend), charset));
		writer.write(Content);
		writer.flush();
		writer.close();
	}

	/**
	 * XML文本文件解析
	 * 
	 * @param content
	 * @throws ParserConfigurationException
	 */
	public static Document xmlAnalysis(String content) throws Exception {
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = dbf.newDocumentBuilder();
		Document document = builder.parse(new InputSource(
				new ByteArrayInputStream(content.getBytes("UTF-8"))));
		return document;
	}


	

	/**
	 * 
	 * @param <V>
	 * @param sourceList
	 * @return
	 */
	public static <V> boolean listIsEmpty(List<V> sourceList) {
		return (sourceList == null || sourceList.size() == 0);
	}

	
	/**
	 * 判断运行环境是否linx
	 */
	public static boolean runtimeIsLinux() {
		String os = System.getProperties().getProperty("os.name");
		return os.contains("Linux");
	}

	/**
	 * 盘符显示处理
	 */
	public static String pathManager(String path) {
		try {
			path = path.substring(path.indexOf("smali"));
		} catch (Exception e) {
		}
		return path;
	}
}
